package com.sdy.supermarket.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.mvc.utils.JsonUtil;
import com.sdy.supermarket.biz.model.GsNotice;
import com.sdy.supermarket.biz.service.GsNoticeService;
import com.sdy.supermarket.biz.service.WorkbenchService;
import com.sdy.supermarket.biz.vo.NoticeSimpleVO;
import com.sdy.usercenter.api.UserQueryApi;
import com.sdy.usercenter.api.dto.ResourceDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WorkbenchServiceImpl implements WorkbenchService {
    @Value("${res.app.code}")
    private String appCode;
    @Autowired
    private GsNoticeService gsNoticeService;
    @Autowired
    private UserQueryApi userQueryApi;

    @Override
    public IPage<NoticeSimpleVO> userNoticePage(Integer userId, Page<NoticeSimpleVO> page) {
        ResourceDTO sys = userQueryApi.getAppByCode(appCode);
        if (sys == null) {
            return page.setTotal(0).setRecords(new ArrayList<>());
        }
        IPage<GsNotice> noticePage = gsNoticeService.page(
                new Page<>(page.getCurrent(), page.getSize()),
                new LambdaQueryWrapper<GsNotice>()
                        .eq(GsNotice::getUserId, userId)
                        .eq(GsNotice::getNoticeStatus, 1)
                        .eq(GsNotice::getNoticePlatformId, sys.getId())
                        .orderByDesc(GsNotice::getNoticeId)
        );
        List<NoticeSimpleVO> noticeSimpleVOList = noticePage.getRecords()
                .stream()
                .map(item -> JsonUtil.convert(item, NoticeSimpleVO.class))
                .collect(Collectors.toList());
        return new Page<NoticeSimpleVO>(page.getCurrent(), page.getSize(), page.getTotal()).setRecords(noticeSimpleVOList);
    }
}
